<template>
  <div>
    <Row>
      <Col span="24">
        <Card>
          <div class="margin-bottom-15">
            <Button type="primary" v-has="'GameType/add'" @click="alertAdd" icon="md-add">{{ $t('add_button') }}
            </Button>
          </div>
          <div>
            <Table row-key="id" :columns="columnsList" :data="tableData" border disabled-hover />
          </div>
        </Card>
      </Col>
    </Row>
    <Modal v-model="modalSetting.show" width="668" :styles="{top: '30px'}" @on-visible-change="doCancel">
      <p slot="header" style="color:#2d8cf0">
        <Icon type="md-alert" />
        <span>{{formItem.id ? '编辑' : '新增'}}分类</span>
      </p>
      <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="80">
        <FormItem label="分类名称" prop="title">
          <Input v-model="formItem.title" placeholder="请输入分类名称" />
        </FormItem>
        <FormItem label="父级分类" prop="fid">
          <Cascader :data="selectOption" placeholder="请选择游戏分类" change-on-select v-model="formItem.fid"></Cascader>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
        <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { getList, changeStatus, add, edit, del, getChooseItem } from '@/api/game-type'

const editButton = (vm, h, currentRow, index) => {
  if (vm.buttonShow.edit) {
    return h('Button', {
      props: {
        type: 'primary'
      },
      style: {
        margin: '0 5px'
      },
      on: {
        'click': () => {
          if (vm.selectOption.length <= 0) {
            getChooseItem().then(response => {
              vm.selectOption = response.data.data.list
              vm.selectOption.unshift({
                value: 0,
                label: '顶级分类'
              })
            })
          }
          vm.formItem.id = currentRow.id
          vm.formItem.title = currentRow.title
          // vm.formItem.fid = currentRow.relation
          vm.modalSetting.show = true
          vm.modalSetting.index = index
        }
      }
    }, vm.$t('edit_button'))
  }
}
const deleteButton = (vm, h, currentRow, index) => {
  if (vm.buttonShow.del) {
    return h('Poptip', {
      props: {
        confirm: true,
        title: '您确定要删除这条数据吗? ',
        transfer: true
      },
      on: {
        'on-ok': () => {
          del(currentRow.id).then(response => {
            vm.getList()
            vm.$Message.success(response.data.msg)
          })
          currentRow.loading = false
        }
      }
    }, [
      h('Button', {
        style: {
          margin: '0 5px'
        },
        props: {
          type: 'error',
          placement: 'top',
          loading: currentRow.isDeleting
        }
      }, vm.$t('delete_button'))
    ])
  }
}
export default {
  name: 'game_type',
  data () {
    return {
      columnsList: [
        {
          title: '分类名称',
          align: 'left',
          key: 'title',
          tree: true
        },
        {
          title: '游戏数量',
          align: 'center',
          key: 'total',
          width: 200
        },
        {
          title: '状态',
          align: 'center',
          key: 'status',
          width: 100,
          render: (h, params) => {
            let vm = this
            return h('i-switch', {
              attrs: {
                size: 'large'
              },
              props: {
                'true-value': 1,
                'false-value': 0,
                value: params.row.status,
                disabled: !vm.buttonShow.changeStatus
              },
              on: {
                'on-change': function (status) {
                  changeStatus(status, params.row.id).then(response => {
                    vm.$Message.success(response.data.msg)
                    vm.getList()
                  })
                }
              }
            }, [
              h('span', {
                slot: 'open'
              }, vm.$t('open_choose')),
              h('span', {
                slot: 'close'
              }, vm.$t('close_choose'))
            ])
          }
        },
        {
          title: '操作',
          align: 'center',
          key: 'handle',
          width: 200,
          render: (h, params) => {
            return h('div', [
              editButton(this, h, params.row, params.index),
              deleteButton(this, h, params.row, params.index)
            ])
          }
        }
      ],
      tableData: [],
      modalSetting: {
        show: false,
        loading: false,
        index: 0
      },
      formItem: {
        title: '',
        fid: [0],
        url: '',
        sort: 0,
        id: 0
      },
      selectOption: [],
      ruleValidate: {
        title: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      buttonShow: {
        edit: true,
        del: true,
        changeStatus: true
      }
    }
  },
  created () {
    let vm = this
    vm.getList()
    vm.hasRule('GameType/edit').then(res => {
      vm.buttonShow.edit = res
    })
    vm.hasRule('GameType/del').then(res => {
      vm.buttonShow.del = res
    })
    vm.hasRule('GameType/changeStatus').then(res => {
      vm.buttonShow.changeStatus = res
    })
  },
  methods: {
    alertAdd () {
      let vm = this
      if (vm.selectOption.length <= 0) {
        getChooseItem().then(response => {
          vm.selectOption = response.data.data.list
          vm.selectOption.unshift({
            value: 0,
            label: '顶级分类'
          })
        })
      }
      vm.modalSetting.show = true
    },
    submit () {
      let vm = this
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          vm.modalSetting.loading = true
          if (vm.formItem.id === 0) {
            add(vm.formItem).then(response => {
              vm.$Message.success(response.data.msg)
              vm.getList()
              vm.cancel()
            }).catch(() => {
              vm.modalSetting.loading = false
            })
          } else {
            edit(vm.formItem).then(response => {
              vm.$Message.success(response.data.msg)
              vm.getList()
              vm.cancel()
            }).catch(() => {
              vm.modalSetting.loading = false
            })
          }
        }
      })
    },
    cancel () {
      this.modalSetting.show = false
    },
    doCancel (data) {
      if (!data) {
        this.formItem.id = 0
        this.$refs['myForm'].resetFields()
        this.modalSetting.loading = false
        this.modalSetting.index = 0
        this.selectOption = []
      }
    },
    getList () {
      let vm = this
      getList().then(response => {
        vm.tableData = response.data.data.list
      })
    }
  }
}
</script>
